package com.da.javatest.leetCode;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author chenlida
 * @date 2019/1/10 16:17
 * @description
 */
public class Code841 {
    public static void main(String[] args) {
        new Code841().canVisitAllRooms(Arrays
            .asList(Collections.singletonList(1), Collections.singletonList(2), Collections.singletonList(3),
                Collections.emptyList()));
    }

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        List<Integer> keys = rooms.get(0);
        Set<Integer> s = new HashSet<>(keys);
        for (int i = 0; i < rooms.size(); i++) {
            Set<Integer> temp = new HashSet<>(s);
            for (Integer index : s) {
                temp.addAll(rooms.get(index));
            }
            s = temp;
        }
        s.remove(0);
        return rooms.size() == s.size() + 1;
    }
}
